package controller.admin;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import pojos.common.DocHelper;
import pojos.common.ReplyMessage;
import service.CommonUtilsService;

import javax.annotation.Resource;

/**
 * 管理员通知管理控制器
 */
@Controller
@RequestMapping(value = {"/admin"})
public class AdminForNotice {

    @Resource(name = "commonUtilsService")
    private CommonUtilsService commonUtilsService;

    /**
     * 查询所有的文档信息
     */
    @PostMapping("/frame/queryAllDoc")
    @ResponseBody
    public ReplyMessage queryAllDocument(){
        return commonUtilsService.queryAllDoc();
    }

    /**
     * 添加文档
     */
    @PostMapping("/frame/add-doc")
    @ResponseBody
    public ReplyMessage addDocument(@RequestParam(value = "role",required = false) Integer role,
                                    @RequestParam(value = "order",required = false) Integer order,
                                    @RequestParam(value = "enable",required = false) Integer isEnable,
                                    @RequestParam(value = "title",required = false) String title,
                                    @RequestParam(value = "content",required = false) String content){
        ReplyMessage replyMessage = new ReplyMessage();

        if(role == null){
            replyMessage.setStatus(400);
            replyMessage.setInfo("请输入适用角色！");
            return replyMessage;
        }

        if(order == null){
            replyMessage.setStatus(400);
            replyMessage.setInfo("请输入正确的排序序号！");
            return replyMessage;
        }

        if(isEnable == null){
            replyMessage.setStatus(400);
            replyMessage.setInfo("请选择是否启用该文档！");
            return replyMessage;
        }

        if(title == null){
            replyMessage.setStatus(400);
            replyMessage.setInfo("文档标题不能为空！");
            return replyMessage;
        }

        if(title.length() < 1 || title.length() > 100){
            replyMessage.setStatus(400);
            replyMessage.setInfo("标题不正确！");
            return replyMessage;
        }

        if(content == null || content.equals("")){
            replyMessage.setStatus(400);
            replyMessage.setInfo("文档内容不能为空！");
            return replyMessage;
        }

        DocHelper docHelper = new DocHelper();
        docHelper.setRole(role);
        docHelper.setOrder(order);
        docHelper.setIsEnable(isEnable);
        docHelper.setTitle(title);
        docHelper.setContent(content);

        return commonUtilsService.addDocument(docHelper);
    }

    /**
     * 修改文档
     */
    @PostMapping("/frame/update-doc-sub")
    @ResponseBody
    public ReplyMessage updateDocument(@RequestParam(value = "id",required = false) Integer id,
                                       @RequestParam(value = "role",required = false) Integer role,
                                       @RequestParam(value = "order",required = false) Integer order,
                                       @RequestParam(value = "enable",required = false) Integer isEnable,
                                       @RequestParam(value = "title",required = false) String title,
                                       @RequestParam(value = "content",required = false) String content){
        ReplyMessage replyMessage = new ReplyMessage();

        if(id == null){
            replyMessage.setStatus(400);
            replyMessage.setInfo("不能获取到关键参数，请刷新页面后重新尝试！");
            return replyMessage;
        }

        if(role == null){
            replyMessage.setStatus(400);
            replyMessage.setInfo("请输入适用角色！");
            return replyMessage;
        }

        if(order == null){
            replyMessage.setStatus(400);
            replyMessage.setInfo("请输入正确的排序序号！");
            return replyMessage;
        }

        if(isEnable == null){
            replyMessage.setStatus(400);
            replyMessage.setInfo("请选择是否启用该文档！");
            return replyMessage;
        }

        if(title == null){
            replyMessage.setStatus(400);
            replyMessage.setInfo("文档标题不能为空！");
            return replyMessage;
        }

        if(title.length() < 1 || title.length() > 100){
            replyMessage.setStatus(400);
            replyMessage.setInfo("标题不正确！");
            return replyMessage;
        }

        if(content == null || content.equals("")){
            replyMessage.setStatus(400);
            replyMessage.setInfo("文档内容不能为空！");
            return replyMessage;
        }

        DocHelper docHelper = new DocHelper();
        docHelper.setId(id);
        docHelper.setRole(role);
        docHelper.setOrder(order);
        docHelper.setIsEnable(isEnable);
        docHelper.setTitle(title);
        docHelper.setContent(content);

        return commonUtilsService.updateDoc(docHelper);
    }
}
